Let us take our simple language

    L = { a, a+a, a+a+a, a+a+a+a, ... }

and add balanced parentheses to it, so L becomes

    L = { a, (a), a+a, (a)+a, a+(a), (a+a), a+a+a, (a+a)+a, a+(a+a), ((a+a)+a)+a, ... }

-----------------------------------------------------------------------------


1.) Here is an ambiguous BNF grammar for this new language.


BNF:    expr -> expr '+' expr | '(' expr ')' | a

-----------------------------------------------------------------------------


2.) Here is an unambiguous, right associative, BNF grammar
(and an EBNF grammar derived from it).


BNF:    expr -> a '+' expr | '(' expr ')' '+' expr | '(' expr ')' | a


EBNF:   expr -> a [ '+' expr ]  |  '(' expr ')' [ '+' expr ]


Here are simplified grammars.

BNF:   expr -> term '+' expr | term
       term -> a | '(' expr ')'

EBNF:  expr -> term [ '+' expr ]
       term -> a | '(' expr ')'

EBNF:  expr -> term ( '+' term )*
       term -> a | '(' expr ')'

Notice how we can use two non-terminals to simplify this grammar,
but they are not required for disambiguating the grammar.

-----------------------------------------------------------------------------


3.) Here is an unambiguous, left associative, BNF grammar
(and an EBNF grammar derived from it).


BNF:    expr ->  expr '+' a | expr + '(' expr ')' | '(' expr ')' | a


EBNF:   expr -> [ expr '+' ] a  |  [ expr '+' ] '(' expr ')'


Here are simplified grammars.

BNF:   expr -> expr '+' term | term
       term -> a | '(' expr ')'

EBNF:  expr -> [ expr '+' ] term
       term -> a | '(' expr ')'

EBNF:  expr -> ( term '+' )* term
       term -> a | '(' expr ')'

EBNF:  expr -> term ( '+' term )*
       term -> a | '(' expr ')'

-----------------------------------------------------------------------------


Since we now using parentheses in our language, we can specify the following
interesting property. We can force + to be a non-associative operator. This
means that the string "a+a+a" would not be allowed in the language. Instead,
parentheses would have(!) to be used when there is more than one + operator.
(For example, in the programming language Maple, the exponentiation
operator, ^, is non-associative and 2^3^4 has no meaning.)


BNF:  expr -> a
            | a '+' a
            | a '+' '(' expr ')'
            | '(' expr ')' '+' a
            | '(' expr ')' '+' '(' expr ')'
            | '(' expr ')'


Some language designers make certain operators non-associative when they feel
that those operators are not universally assumed to be either left or right
associative (like the exponentiation operator). Forcing programmers to use
parentheses is thought to be safer than letting them write code without the
parentheses, but having the parser use the associativity that is opposite
to what the programmer thinks it is.


Here is a simplified non-associative operator grammar.

BNF:  expr -> term '+' term | term
      term -> a | '(' expr ')'

What happens when you try to parse "a+a+a"?

-----------------------------------------------------------------------------